<?php

namespace App\Http\Controllers;

use App\Models\Product;
use Illuminate\Http\Request;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Illuminate\Support\Facades\Log;

class MediaController extends Controller
{
    /**
     * 上传商品图片
     */
    public function store(Request $request, Product $product)
    {
        try {
            Log::info('开始处理图片上传请求', [
                'has_file' => $request->hasFile('image'),
                'all_data' => $request->all(),
                'files' => $request->allFiles(),
                'content_type' => $request->header('Content-Type')
            ]);

            $request->validate([
                'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048'
            ]);

            if (!$request->hasFile('image')) {
                Log::error('没有接收到图片文件');
                return response()->json(['error' => '没有接收到图片文件'], 400);
            }

            $file = $request->file('image');
            Log::info('图片文件信息', [
                'original_name' => $file->getClientOriginalName(),
                'mime_type' => $file->getMimeType(),
                'size' => $file->getSize()
            ]);

            $media = $product->addMediaFromRequest('image')
                ->toMediaCollection('product-images');

            Log::info('图片上传成功', [
                'media_id' => $media->id,
                'url' => $media->getUrl()
            ]);

            if ($request->ajax() || $request->wantsJson()) {
                return response()->json([
                    'message' => '图片上传成功',
                    'media' => [
                        'id' => $media->id,
                        'name' => $media->name,
                        'original_url' => $media->getUrl(),
                    ]
                ]);
            }

            return back()->with('success', '图片上传成功');
        } catch (\Exception $e) {
            Log::error('图片上传失败', [
                'error' => $e->getMessage(),
                'trace' => $e->getTraceAsString(),
                'request' => $request->all()
            ]);
            
            return response()->json([
                'error' => '图片上传失败: ' . $e->getMessage()
            ], 500);
        }
    }

    /**
     * 删除商品图片
     */
    public function destroy($media)
    {
        try {
            // 添加请求日志
            Log::info('收到删除图片请求', [
                'media_id' => $media,
                'request_method' => request()->method(),
                'headers' => request()->headers->all()
            ]);

            $media = Media::findOrFail($media);
            $media->delete();
            
            if (request()->wantsJson()) {
                return response()->json(['message' => '图片删除成功']);
            }
            
            return back()->with('success', '图片删除成功');
        } catch (\Exception $e) {
            Log::error('删除图片失败', [
                'error' => $e->getMessage(),
                'trace' => $e->getTraceAsString()
            ]);
            
            return response()->json([
                'error' => '删除失败: ' . $e->getMessage()
            ], 500);
        }
    }
} 